<?php

namespace app\admin\model;

use think\Model;
use think\Cache;

class Area extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = false;
    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $resultSetType = 'collection';

    /**
     * @param $name
     * @param null $pid
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getId($name,$pid = null)
    {
        $where = $pid ? ['name'=>$name,'pid'=>$pid] : ['name'=>$name];
        $ret = self::where($where)->field('id')->find();
        return $ret->id;
    }

    public static function getArea()
    {
        if(!$list = Cache::get('arealist'))
        {
            $area = Area::field('id,name')->select();
            $area = $area->toArray();
            $list = array_column($area,'name','id');
            Cache::set('areaList',$list,0);//设置0永久缓存不过期
        }
        return $list;
    }
}
